/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webbucks;

//import java.io.IOException;
//import java.io.PrintWriter;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

/**
 *
 * @author scartissue
 */
public class Register extends HttpServlet {
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String driver = "com.mysql.jdbc.Driver";
        String connectionURL = "jdbc:mysql://localhost:8889/webbucks";
        Statement statement = null;
        int updateQuery = 0;
        Connection connection = null;
        
        try {            
            // Load the database driver
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            // Get a Connection to the database
            connection = DriverManager.getConnection(connectionURL, "root", "root");

            /* createStatement() is used for create statement object that is
             * used for sending sql statements to the specified database. */
            statement = connection.createStatement();

            //Get data from bean
            String uname = request.getParameter("uname");
            String pass = request.getParameter("pass");
            String confirmPass = request.getParameter("confirmPassword");
            String mail = request.getParameter("mail");
            String fName = request.getParameter("fName");
            String lName = request.getParameter("lName");
            String persNumber = request.getParameter("persNumber");
            String str = request.getParameter("str");
            int strNumber = Integer.parseInt(request.getParameter("strNumber"));
            String cit = request.getParameter("cit");
            int postNumber = Integer.parseInt(request.getParameter("postNumber")); 

            //Add the data into the database
            String QueryString = "INSERT INTO customer(username, password, email, firstname, lastname, personalnumber, street, streetnumber, city, postalnumber)"
                    + "VALUES('" + uname + "','" + pass + "','" + mail + "','" + fName  + "','" + lName + "','" + persNumber + "','" + str + "'," + strNumber + ",'" + cit + "'," + postNumber + ")";
            
            out.println(QueryString);
            
            out.println("Test 1");
            updateQuery = statement.executeUpdate(QueryString);
            out.println("Test 2");

            if (updateQuery != 0) {
                out.println("table is created successfully");
            }
            else{
                out.println("table not created");
            }

            out.println("Test 3");
            statement.close();
        }
        catch(ClassNotFoundException e){
            out.println("Couldn't load database driver: "
                    + e.getMessage());
        }
        catch(SQLException e){
            out.println("SQLException caught: "
                    + e.getMessage());
        }
        catch (Exception e){
            out.println(e);
        }
        finally {
            // Always close the database connection.
            try {
                if (connection != null) connection.close();
                if (statement != null) statement.close();
            }
            catch (SQLException ignored){
                out.println(ignored);
            }
        }
    }
    
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}









//Password:
//Conf Pass:
//Email:
//First name:
//Last Name:
//Personal Number:
//Street:
//Street Number:
//City:
//Postal Number: 



//            "username: " +        request.getParameter("username") + "\n" +
//            "Password: " +        request.getParameter("password") + "\n" +
//            "Conf Pass: " +       request.getParameter("confirmPassword") + "\n" +
//            "Email: " +           request.getParameter("eamil") + "\n" +
//            "First name: " +      request.getParameter("firstName") + "\n" +
//            "Last Name: " +       request.getParameter("lastName") + "\n" +
//            "Personal Number: " + request.getParameter("personalNumber") + "\n" +
//            "Street: " +          request.getParameter("street") + "\n" +
//            "Street Number: " +   request.getParameter("streetNumber") + "\n" +
//            "City: " +            request.getParameter("city") + "\n" +
//            "Postal Number: " +   request.getParameter("postalNumber") + "\n" 